package com.haoyu.number.tree;

/**
 * @author: 蒿雨
 * @create: 2022-01-14 19:05
 * @description:
 */
public class BinaryTreeDemo {

    public static void main(String[] args) {
        //先创建一棵二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建需要的节点
        HeroNode root = new HeroNode(1, "松江");
        HeroNode heroNode2 = new HeroNode(2, "吴用");
        HeroNode heroNode3 = new HeroNode(3, "卢俊义");
        HeroNode heroNode4 = new HeroNode(4, "林冲");
        HeroNode heroNode5 = new HeroNode(5, "关胜");

        //说明，我们先手动创建二叉树，后面我们学习递归的方式创建二叉树
        root.setLeft(heroNode2);
        root.setRight(heroNode3);
        heroNode3.setRight(heroNode4);
        heroNode3.setLeft(heroNode5);
        binaryTree.setRoot(root);
        //1,2,3,5,4
        System.out.println("前序遍历");
        binaryTree.priOrder();
        //2,1,5,3,4
        System.out.println("中序遍历");
        binaryTree.infixOrder();
        //2,5,4,3,1
        System.out.println("后序遍历");
        binaryTree.postOrder();

    }
}

//定义BinaryTree 二叉树
class BinaryTree {
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    //前序遍历
    public void priOrder() {
        if (this.root != null) {
            this.root.preOrder();
        } else {
            System.out.println("二叉树为空，无法遍历");
        }
    }

    //中序遍历
    public void infixOrder() {
        if (this.root != null) {
            this.root.infixOrder();
        } else {
            System.out.println("二叉树为空，无法遍历");
        }
    }

    //后序遍历
    public void postOrder() {
        if (this.root != null) {
            this.root.postOrder();
        } else {
            System.out.println("二叉树为空，无法遍历");
        }
    }
}


//先创建heroNode节点
class HeroNode {
    private int no;
    private String name;
    //默认null
    private HeroNode left;
    private HeroNode right;

    public HeroNode(int no, String name) {
        super();
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
//递归删除节点
    //	如果删除的节点时叶子节点，则删除该节点
//	如果删除的节点是非叶子节点，则删除该子树

    public void delNode(int no){


    }

    //编写前序遍历的方法
    public void preOrder() {
        //先输出父节点
        System.out.println(this);
        //递归向左子树前序遍历
        if (this.left != null) {
            this.left.preOrder();
        }
        //向右递归遍历
        if (this.right != null) {
            this.right.preOrder();
        }

    }

    //中序遍历
    public void infixOrder() {
        //递归向左子树中序遍历
        if (this.left != null) {
            this.left.infixOrder();
        }
        //输出父节点
        System.out.println(this);

        //递归向右子树遍历
        if (this.right != null) {
            this.right.infixOrder();
        }

    }

    //后续遍历
    public void postOrder() {
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }
        System.out.println(this);
    }
    //前序遍历查找

    /**
     * @param no
     * @return如果找到就返回node,否则返回null
     */

    public HeroNode preOrderserch(int no) {
        //比较当前节点是不是
        if (this.no == no) {
            return this;
        }
        //1.则判断当前节点的左子节点是否为空，如果不为空，则递归前序查找
        //2.如果左递归前序查找，找到节点，则返回
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.preOrderserch(no);
        }
        //说明我们左子树查找到
        if (resNode != null) {
            return resNode;
        }
        //左递归前序查找，找到节点，则返回，否则继续判断，当前的节点的右子节点是否为空，如果不空，则继续向右递归前序查找。
        if (this.right != null) {
            resNode = this.right.preOrderserch(no);
        }
        return resNode;
    }


}
